import { getUserInfo } from '../../request/index.js';

Page({
  data: {
    isLogin: null,
    firstLogin: null,
    doctors: 3,
    notifications: 10,
    list:[
      {
        title: '添加就诊人',
        path: '../patient/addPatient'
      },
      {
        title: '门诊预约记录',
        path: ''
      },
      {
        title: '处方信息',
        path: ''
      },
      {
        title: '病历',
        path: ''
      },
      {
        title: '意见反馈',
        path: ''
      },
      {
        title: '修改密码',
        path: ''
      },
    ],
    patient: null
  },
  onShow(){
    if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        })
      }
      var isLogin = getApp().globalData.isLogin;
      this.setData({isLogin: isLogin});
      if(this.data.isLogin){
        getUserInfo({order: getApp().globalData.order}).then(res => {
          if(res.message == 'OK'){
            let app = getApp();
            app.globalData.patients[0].name = res.data.name;
            app.globalData.patients[0].sex = res.data.sex;
            app.globalData.patients[0].id = res.data.id;
            app.globalData.patients[0].num = res.data.phone;
          }
          else{
            wx.showToast({
              title: '获取用户信息失败',
              icon: 'error'
            });
          }
        })
      }
      let _this = this;
      setTimeout(() => {
        let patients = getApp().globalData.patients;
        let patient = getApp().globalData.selectedPatient;
        _this.setData({patient: patients[patient]});
      }, 100);
  },
  toLogin(){
    wx.navigateTo({
      url: '../login/login',
    });
  },
  toPath(event){
    if(!this.data.isLogin){
      wx.showToast({
        title: '请先登录',
        duration: 1000,
        icon: 'none'
      });
      return;
    }
    let path = event.currentTarget.dataset.path;
    wx.navigateTo({
      url: path
    });
  },
  toManage(){
    wx.navigateTo({
      url: '../patient/managePatient',
    })
  },
  logout(){
    let _this = this;
    wx.showModal({
      title: '提示',
      content: '确认退出登录吗？',
      success(res){
        if(res.confirm){
          getApp().globalData.isLogin = false;
          getApp().globalData.userId = null;
          _this.setData({isLogin: false});
          wx.switchTab({
            url: '../index/index',
          });
        }
      }
    })
  }
})